extends Node2D
@onready var test_2: Node2D = $"."
@onready var animatable_body_2d: AnimatableBody2D = $AnimatableBody2D
@onready var collision_shape_2d: CollisionShape2D = $AnimatableBody2D/CollisionShape2D
@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatableBody2D/AnimatedSprite2D
@onready var label: Label = $Label

var my_position: int = 0
var my_image: Image

@export var image_name: String = "attack_1"
signal my_signal(info: Vector2, size: Vector2)


func _ready() -> void:
	pass


func _process(delta: float) -> void:
	pass


func _on_animatable_body_2d_input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void:
	var target_position = get_local_mouse_position()
	var sss             = animatable_body_2d.position-Vector2(my_image.get_width()/2, my_image.get_height()/2)
	var aaa             = target_position-sss
	var bbb             = my_image.get_pixelv(aaa)
	if bbb.r!=0 ||  bbb.g!=0 ||  bbb.b!=0:
		if event is InputEventMouseButton:
			if event.is_pressed() and event.button_index ==  MouseButton.MOUSE_BUTTON_LEFT:
				emit_signal("my_signal", test_2.position, Vector2(my_image.get_width(), my_image.get_height()))


func _on_animated_sprite_2d_animation_looped() -> void:
	my_position = 0
	
func _on_animated_sprite_2d_frame_changed() -> void:
	my_image = animated_sprite_2d.sprite_frames.get_frame_texture(image_name, my_position).get_image()
	my_position += 1


func setImage(path: String, name: String, role_name: String) -> void:
	image_name=name
	var sprite_frames = load(path)
	animated_sprite_2d.sprite_frames = sprite_frames
	animated_sprite_2d.play(image_name)
	my_image = animated_sprite_2d.sprite_frames.get_frame_texture(image_name, 0).get_image()
	var aaa = RectangleShape2D.new()
	aaa.size=Vector2(my_image.get_width(), my_image.get_height())
	collision_shape_2d.shape=aaa
	label.text=role_name
	label.position=Vector2(0-(label.size.x/2), my_image.get_height()/2)
